package com.genericdevice.platform.pages.plugins.mvp;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.genericdevice.platform.R;
import com.genericdevice.platform.core.data.DataBase;
import com.genericdevice.platform.core.mvp.AbstractView;
import com.genericdevice.platform.core.plugins.PluginsLoader;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Xilent on 18.05.2014.
 */
public class PluginsView extends AbstractView<PluginsPresenter> {

    private ListView pluginsList;
    private List<Map<String, Object>> plugins;

    @Override
    public int getViewResource() {
        return R.layout.activity_plugins;
    }

    @Override
    protected void loadElements() {
        pluginsList = (ListView) activity.findViewById(R.id.plugins_list);
        pluginsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                presenter.onPluginSelected(position);
            }
        });
    }

    @Override
    public void init() {
        refresh();
    }

    public void refresh() {
        List<Map<String, Object>> plugins = PluginsLoader.getPluginsList();

        List<String> pluginNames = new ArrayList<String>();

        for (Map<String, Object> plugin : plugins) {
            pluginNames.add((String) plugin.get(DataBase.PLUGINS_NAME_FIELD));
        }

        ArrayAdapter adapter = new ArrayAdapter<String>(activity, android.R.layout.simple_list_item_1, pluginNames);
        pluginsList.setAdapter(adapter);
    }
}
